﻿using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.WebAPI.Application.Command.SheepCommand;
using IntelligentAgriculture.WebAPI.Application.Command.UnitCommand;
using IntelligentAgriculture.WebAPI.Application.Command.UserCommand;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace IntelligentAgriculture.WebAPI.Controllers
{
    /// <summary>
    /// 单位设置
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UnitController : ControllerBase
    {

        private readonly IUnitService unitService;
        private readonly TokenService tokenService;
        private readonly ILogger<UnitController> logger;
        private readonly IMediator mediator;


        public UnitController(IUnitService unitService
            , TokenService tokenService
            , ILogger<UnitController> logger, IMediator mediator)
        {
            this.unitService = unitService;
            this.tokenService = tokenService;
            this.logger = logger;
            this.mediator = mediator;
           
        }

        /// <summary>
        /// 省
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Results<List<Province>>> ProvinceList()
        {
            var command = new UnitProviceCommand();
            return await mediator.Send(command);
        }

        /// <summary>
        /// 市
        /// </summary>
        /// <param name="proVinceId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Results<List<City>>> CityList(int proVinceId)
        {
            var command = new UnitCityCommand(proVinceId);
            return await mediator.Send(command);
        }

        /// <summary>
        /// 县/区
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Results<List<Area>>> UnitList(int cityId)
        {
            var command = new UnitAreaCommand(cityId);
            return await mediator.Send(command);
        }


        /// <summary>
        /// 单位添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> AddUnit(UnitCreateCommand command)
        {
            return await mediator.Send(command);
        }



        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<string>> UploadFile(IFormFile file)
        {
            if (file != null && file.Length > 0)
            {
                // 指定图片上传目录
                var uploadDir = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Image");

                // 如果目录不存在则创建
                if (!Directory.Exists(uploadDir))
                {
                    Directory.CreateDirectory(uploadDir);
                }

                // 生成唯一的文件名
                var uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName;

                // 图片保存路径
                var filePath = Path.Combine(uploadDir, uniqueFileName);

                // 使用文件流将图片保存到指定路径
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }
                var relativePath = Path.Combine(uniqueFileName);
                // 可以将 filePath 存入数据库或返回视图显示图片等操作
                return Results<string>.DataResult(relativePath);
            }

            return Results<string>.FailResult();
        }


        /// <summary>
        /// 查询分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<PageOutput<UnitListOutPut>>> SheepList(UnitListInPut input)
        {
            var command = new UnitQueryCommand(input);
            var roles = await mediator.Send(command);
            return roles;
        }




    }


}
